import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/models/authorization_community_entity.dart';
import 'package:kkhousekeep/net/authorization_connect.dart';
import 'package:kkhousekeep/page/authorization/home_page.dart';
import 'package:kkhousekeep/page/login/login_controller.dart';

import '../../net/respnse_code.dart';
import '../../tools/dialog_util.dart';
import '../../tools/ip_address_unit.dart';
import '../home/home_main_page.dart';
import 'city_page.dart';

class AuthorizationController extends GetxController {
  final bool isJumpToMePage;

  ///搜索栏控制器
  late TextEditingController searchTextController;

  ///经过转换的城市
  var city = "".obs;

  ///实际城市
  String currentCity = "";

  ///社区列表
  var communityList = <Community>[].obs;

  ///是否显示“全部社区”
  var isShowTip = false.obs;

  ///社区是否为空
  var isCommunityEmpty = false.obs;
  late AuthorizationConnect connect;

  AuthorizationController({this.isJumpToMePage = false});

  @override
  void onInit() {
    searchTextController = TextEditingController();
    connect = Get.put(AuthorizationConnect());
    loadData();
    super.onInit();
  }

  ///情况搜索文本
  void clearText() {
    searchTextController.text = "";
    handleCommunitiesResponse();
  }

  void buildCityPage() {
    FocusManager.instance.primaryFocus?.unfocus();
    Get.dialog(const BuildCityPage(), barrierColor: Colors.white);
  }

  void setCity(String newCity) {
    searchTextController.text = "";
    Get.back();
    if (newCity.length > 3) {
      city.value = newCity.replaceRange(2, newCity.length, "...");
      currentCity = newCity;
      handleCommunitiesResponse();
      return;
    }
    currentCity = newCity;
    city.value = newCity;
    handleCommunitiesResponse();
  }

  ///构建选项社区详情页面
  void buildRoomPage(Community community) {
    FocusManager.instance.primaryFocus?.unfocus();
    Get.to(() => BuildAuthorizationHomePage(
          city: currentCity,
          community: community,
        ));
  }

  ///加载数据
  void loadData() async {
    //根据ip地址获取城市名称
    IpAddressData? addressData = await IPAddressUtil.getIPAddress();
    currentCity = addressData?.city ?? "城市数据为空";
    //城市名转换
    if (currentCity.length > 3) {
      city.value = currentCity.replaceRange(2, currentCity.length, "...");
      return;
    }
    city.value = currentCity;
    handleCommunitiesResponse();
  }

  ///根据城市和社区名称获取
  Future<void> handleCommunitiesResponse({String keyword = ""}) async {
    var response = await connect.getCommunities(currentCity, keyword);
    isShowTip.value = false;
    if (response == null) {
      return;
    }
    if (response.code == ResponseCode.fail) {
      DialogUtil.showNetworkFail();
      return;
    }
    if (response.code == ResponseCode.success) {
      if (response.obj.isEmpty) {
        isCommunityEmpty.value = true;
        return;
      }
      if (keyword != "") {
        isShowTip.value = true;
      }
      isCommunityEmpty.value = false;
      communityList.clear();
      for (var community in response.obj) {
        communityList.add(community);
      }
      return;
    }
  }

  void processBack() {
    if (isJumpToMePage) {
      Get.offAll(
        () => const HomeMainPage(
          index: 1,
          userType: UserType.tenant,
        ),
      );
    } else {
      Get.back();
    }
  }
}
